/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentation;

import DataAccess.entity.Categoria;
import DataAccess.entity.Clientefornitore;
import DataAccess.entity.Indirizzo;
import DataAccess.session.CategoriaFacade;
import DataAccess.session.ClientefornitoreFacade;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Calendar;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JTextField;

/**
 *
 * @author Domenico
 */
public class ClienteFornitoreForm extends javax.swing.JFrame {
    private Categoria categoria;
    private List<Categoria> listaCategorie;
    private String clientefornitore;
   // private DataAccess.entity.Indirizzo indirizzoAttuale;
    //private DataAccess.entity.Indirizzo IndirizzoFiscale;
    //private DataAccess.entity.Indirizzo IndirizzoTitolare;
    private IndirizzoForm indirizzoForm;
    private Clientefornitore clientefornitoreEntity = new Clientefornitore();
    
    public ClienteFornitoreForm(String tipo, Clientefornitore cf) {
        clientefornitoreEntity = cf;
        clientefornitore = tipo;
        initComponents();
        inizialiazza();
        setVisible(true);
    }
    private void inizialiazza() {
        jButton2.setVisible(false);
        jTextField1.setInputVerifier(new InputVerifierNotEmpty("Denominazione"));
        jTextField2.setInputVerifier(new InputVerifierNotEmpty("Partita IVA"));
        jTextField3.setInputVerifier(new InputVerifierNotEmpty("Indirizzo Fiscale"));
        jTextField11.setInputVerifier(new InputVerifierNotEmpty("Indirizzo Attuale"));
        jTextField4.setInputVerifier(new InputVerifierNotEmpty("Nome Titolare"));
        jTextField5.setInputVerifier(new InputVerifierNotEmpty("Cognome Titolare"));
        jTextField7.setInputVerifier(new InputVerifierCF("Codice Fiscale"));
        jTextField12.setInputVerifier(new InputVerifierNotEmpty("Indirizzo Titolare"));
        
        jTextField13.setInputVerifier(new InputVerifierNotEmpty("Numero Documento"));
        jTextField14.setInputVerifier(new InputVerifierNotEmpty("Tipo Documento"));
        jTextField15.setInputVerifier(new InputVerifierNotEmpty("Città Documento"));
        jTextField16.setInputVerifier(new InputVerifierNotEmpty("Provincia Documento"));
        jTextField6.setInputVerifier(new InputVerifierNotEmpty("Città Nascita"));
        jTextField8.setInputVerifier(new InputVerifierNotEmpty("email"));
        jTextField9.setInputVerifier(new InputVerifierNotEmpty("Telefono"));
        //jTextField10.setInputVerifier(new InputVerifierNotEmpty("Note"));
        jXDatePicker1.setInputVerifier(new InputVerifierNotEmpty("Data di Nascita"));
        jXDatePicker2.setInputVerifier(new InputVerifierNotEmpty("Data Documento"));
        jXDatePicker3.setInputVerifier(new InputVerifierNotEmpty("Data"));
        //jComboBox1.setInputVerifier(new InputVerifierNotEmpty("Partita IVA"));
        
        jTextField1.setText(clientefornitoreEntity.getDenominazione());
        jTextField10.setText(clientefornitoreEntity.getNote());
     aggiornaCampoIndirizzo(jTextField11, clientefornitoreEntity.getIndirizzoAttuale());
     aggiornaCampoIndirizzo(jTextField12, clientefornitoreEntity.getIndirizzoTitolare());
     jTextField13.setText(clientefornitoreEntity.getNumeroDoc());
     jTextField14.setText(clientefornitoreEntity.getTipoDoc());
     jTextField15.setText(clientefornitoreEntity.getCittaDoc());
     jTextField16.setText(clientefornitoreEntity.getProvinciaDoc());
     jTextField17.setText("bo");
     jTextField2.setText(clientefornitoreEntity.getPartitaIVA());
     aggiornaCampoIndirizzo(jTextField3, clientefornitoreEntity.getIndirizzoFiscale());
     jTextField4.setText(clientefornitoreEntity.getNomeTitolare());
     jTextField5.setText(clientefornitoreEntity.getCognomeTitolare());
     jTextField6.setText(clientefornitoreEntity.getCittaNascita());
     jTextField7.setText(clientefornitoreEntity.getCodiceFiscale());
     jTextField8.setText(clientefornitoreEntity.getEmail());
     jTextField9.setText(clientefornitoreEntity.getTelefono());
     if (clientefornitoreEntity.getDataNascita() != null) {
        jXDatePicker1.setDate(clientefornitoreEntity.getDataNascita().getTime());
     }
     if (clientefornitoreEntity.getDataDoc() != null) {
        jXDatePicker2.setDate(clientefornitoreEntity.getDataDoc().getTime());
     }
     if (clientefornitoreEntity.getData() != null) {
        jXDatePicker3.setDate(clientefornitoreEntity.getData().getTime());
     } else {
         jXDatePicker3.setDate(Calendar.getInstance().getTime());
     }
     jComboBox1.removeAllItems();
     CategoriaFacade cf = new CategoriaFacade();
     listaCategorie = cf.findAll();
     for (Categoria categoria : listaCategorie) {
        jComboBox1.addItem(categoria.getNomeCategoria()); 
        
     }
        jComboBox1.addItem("Aggiungi Categoria"); 
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jTextField7 = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();
        jTextField9 = new javax.swing.JTextField();
        jTextField10 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jTextField11 = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jTextField12 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jTextField13 = new javax.swing.JTextField();
        jTextField14 = new javax.swing.JTextField();
        jTextField15 = new javax.swing.JTextField();
        jTextField16 = new javax.swing.JTextField();
        jTextField17 = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        jXDatePicker1 = new org.jdesktop.swingx.JXDatePicker();
        jXDatePicker2 = new org.jdesktop.swingx.JXDatePicker();
        jButton3 = new javax.swing.JButton();
        jTextField6 = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        jXDatePicker3 = new org.jdesktop.swingx.JXDatePicker();
        jLabel22 = new javax.swing.JLabel();

        setAlwaysOnTop(true);

        jLabel1.setText("Denominazione: ");

        jLabel2.setText("Partita IVA: ");

        jLabel3.setText("Categoria: ");

        jLabel4.setText("Nome Titolare: ");

        jLabel5.setText("Cognome Titolare: ");

        jLabel6.setText("Data di nascita:");

        jLabel7.setText("Sesso:");

        jLabel8.setText("Codice Fiscale:");

        jLabel9.setText("Telefono: ");

        jLabel10.setText("email:");

        jLabel11.setText("Note:");

        jLabel12.setText("Indirizzo Fiscale: ");

        jTextField1.setText("jTextField1");
        jTextField1.setNextFocusableComponent(jTextField2);

        jTextField2.setText("jTextField2");
        jTextField2.setNextFocusableComponent(jComboBox1);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setNextFocusableComponent(jTextField3);
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jTextField3.setEditable(false);
        jTextField3.setText("jTextField3");
        jTextField3.setNextFocusableComponent(jTextField11);
        jTextField3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField3MouseClicked(evt);
            }
        });
        jTextField3.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField3FocusGained(evt);
            }
        });

        jTextField4.setText("jTextField4");
        jTextField4.setNextFocusableComponent(jTextField5);

        jTextField5.setText("jTextField5");
        jTextField5.setNextFocusableComponent(jXDatePicker1);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Maschio", "Femmina" }));
        jComboBox2.setNextFocusableComponent(jTextField7);

        jTextField7.setText("jTextField7");
        jTextField7.setNextFocusableComponent(jTextField12);

        jTextField8.setText("jTextField8");

        jTextField9.setText("jTextField9");

        jTextField10.setText("jTextField10");
        jTextField10.setNextFocusableComponent(jButton1);

        jLabel13.setText("Indirizzo Attuale: ");

        jTextField11.setEditable(false);
        jTextField11.setText("jTextField11");
        jTextField11.setNextFocusableComponent(jTextField4);
        jTextField11.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField11MouseClicked(evt);
            }
        });

        jLabel14.setText("Indirizzo titolare: ");

        jTextField12.setEditable(false);
        jTextField12.setText("jTextField12");
        jTextField12.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField12MouseClicked(evt);
            }
        });

        jButton1.setText("Aggiungi Cliente");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Annulla");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel15.setText("Numero Documento:");

        jLabel16.setText("Tipo Documento:");

        jLabel17.setText("Città Documento:");

        jLabel18.setText("Provincia Documento:");

        jLabel19.setText("Verifica Codice Fiscale:");

        jTextField13.setText("jTextField13");

        jTextField14.setText("jTextField14");

        jTextField15.setText("jTextField15");

        jTextField16.setText("jTextField16");
        jTextField16.setNextFocusableComponent(jXDatePicker2);

        jTextField17.setText("jTextField17");
        jTextField17.setNextFocusableComponent(jTextField8);

        jLabel20.setText("Data Documento:");

        jXDatePicker1.setLinkPanel(null);
        jXDatePicker1.setNextFocusableComponent(jTextField6);
        jXDatePicker1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jXDatePicker1PropertyChange(evt);
            }
        });

        jXDatePicker2.setNextFocusableComponent(jTextField17);
        jXDatePicker2.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jXDatePicker2PropertyChange(evt);
            }
        });

        jButton3.setText("Stampa");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTextField6.setText("jTextField6");
        jTextField6.setNextFocusableComponent(jComboBox2);

        jLabel21.setText("Città di Nascita:");

        jXDatePicker3.setLinkPanel(null);
        jXDatePicker3.setNextFocusableComponent(jTextField1);

        jLabel22.setText("Data:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel14)
                            .addComponent(jLabel19)
                            .addComponent(jLabel17)
                            .addComponent(jLabel18)
                            .addComponent(jLabel15)
                            .addComponent(jLabel16)
                            .addComponent(jLabel20))
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField17, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField12)
                            .addComponent(jXDatePicker2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jTextField13, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextField14, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextField16, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextField15, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel21)
                                .addGap(75, 75, 75)
                                .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addGap(124, 124, 124)
                                .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, 399, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel10))
                                .addGap(102, 102, 102)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel12)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel13))
                                .addGap(60, 60, 60)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jTextField11, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jXDatePicker1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel22))
                                .addGap(72, 72, 72)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jXDatePicker3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXDatePicker3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel22))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jXDatePicker1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel21))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(jTextField12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(jTextField13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(jTextField14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(jTextField15, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18)
                    .addComponent(jTextField16, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(jXDatePicker2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(jTextField17, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       if (validaCampi()) {
           //clientefornitoreEntity = new Clientefornitore();
           clientefornitoreEntity.setCategoria(listaCategorie.get(jComboBox1.getSelectedIndex()));
           clientefornitoreEntity.setCittaDoc(jTextField15.getText());
           clientefornitoreEntity.setClienteFornitore(this.clientefornitore);
           clientefornitoreEntity.setCodiceFiscale(jTextField7.getText());
           clientefornitoreEntity.setCognomeTitolare(jTextField5.getText());
           
           Calendar valoreDataNascita = Calendar.getInstance();
           valoreDataNascita.setTime(jXDatePicker1.getDate());
           clientefornitoreEntity.setDataNascita(valoreDataNascita);
           Calendar valoreDataDoc = Calendar.getInstance();
           valoreDataDoc.setTime(jXDatePicker2.getDate());
           clientefornitoreEntity.setDataDoc(valoreDataDoc);
           Calendar valoreData = Calendar.getInstance();
           valoreData.setTime(jXDatePicker3.getDate());
           clientefornitoreEntity.setDataDoc(valoreData);
           
           clientefornitoreEntity.setDenominazione(jTextField1.getText());
           clientefornitoreEntity.setEmail(jTextField9.getText());
          
           clientefornitoreEntity.setNomeTitolare(jTextField4.getText());
           clientefornitoreEntity.setNote(jTextField10.getText());
           
           clientefornitoreEntity.setNumeroDoc(jTextField13.getText());
           clientefornitoreEntity.setPartitaIVA(jTextField2.getText());
           clientefornitoreEntity.setProvinciaDoc(jTextField16.getText());
           clientefornitoreEntity.setSesso(jComboBox2.getSelectedItem().toString());
           clientefornitoreEntity.setTelefono(jTextField8.getText());
           clientefornitoreEntity.setTipoDoc(jTextField14.getText());
           clientefornitoreEntity.setVerificaCodiceFiscale(jTextField17.getText());
           clientefornitoreEntity.setCittaNascita(jTextField6.getText());
           
           ClientefornitoreFacade cfFacade = new ClientefornitoreFacade();
           if (clientefornitoreEntity.getIdClienteFornitore() == null) {
               cfFacade.create(clientefornitoreEntity);
           } else {
                cfFacade.update(clientefornitoreEntity);
           }
           this.dispose();
           
           //this.setVisible(false);
           /*
           em.getTransaction().begin();

Player p1 = new Player();
p1.setId(5);
p1.setFirstname("Ian");
p1.setJerseynumber(30);
p1.setLastname("Thorpe");
p1.setLastspokenwords("I am in the best form");
em.persist(p1);

Player p2 = new Player();
p2.setId(6);
p2.setFirstname("Deigo");
p2.setJerseynumber(40);
p2.setLastname("Maradona");
p2.setLastspokenwords("I will be back");
em.persist(p2);

em.getTransaction().commit();

em.close();
emf.close();
**/
       }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField3FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField3FocusGained
        
    }//GEN-LAST:event_jTextField3FocusGained

    private void jTextField3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField3MouseClicked
        this.setEnabled(false);
        indirizzoForm = new IndirizzoForm(clientefornitoreEntity.getIndirizzoFiscale());
        if (indirizzoForm == null) {
            return;
        }
        indirizzoForm.addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent e) {
                
            }

            @Override
            public void windowClosing(WindowEvent e) {
                
            }

            @Override
            public void windowClosed(WindowEvent e) {
               
                Indirizzo indirizzo = indirizzoForm.getIndirizzo();
                if (indirizzo != null) {
                    
                    clientefornitoreEntity.setIndirizzoFiscale(indirizzo);
                }
                aggiornaCampoIndirizzo(jTextField3, indirizzo);
                abilita();
            }

            @Override
            public void windowIconified(WindowEvent e) {
                
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
                
            }

            @Override
            public void windowActivated(WindowEvent e) {
                
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
                
            }
        });
        
        
        
    }//GEN-LAST:event_jTextField3MouseClicked

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        if (jComboBox1.getSelectedIndex() == (jComboBox1.getItemCount() - 1)) {
            
        }
            
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jTextField11MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField11MouseClicked
        this.setEnabled(false);
        indirizzoForm = new IndirizzoForm(clientefornitoreEntity.getIndirizzoAttuale());
        if (indirizzoForm == null) {
            return;
        }
        indirizzoForm.addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent e) {
                
            }

            @Override
            public void windowClosing(WindowEvent e) {
                
            }

            @Override
            public void windowClosed(WindowEvent e) {
                Indirizzo indirizzo = indirizzoForm.getIndirizzo();
                if (indirizzo != null) {
                    clientefornitoreEntity.setIndirizzoAttuale(indirizzo);
                }
                aggiornaCampoIndirizzo(jTextField11, indirizzo);
                abilita();
            }

            @Override
            public void windowIconified(WindowEvent e) {
                
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
                
            }

            @Override
            public void windowActivated(WindowEvent e) {
                
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
                
            }
        });
    }//GEN-LAST:event_jTextField11MouseClicked

    private void jTextField12MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField12MouseClicked
       
        this.setEnabled(false);
        indirizzoForm = new IndirizzoForm(clientefornitoreEntity.getIndirizzoTitolare());
        
        if (indirizzoForm == null) {
            return;
        }
        indirizzoForm.addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent e) {
                
            }

            @Override
            public void windowClosing(WindowEvent e) {
                
            }

            @Override
            public void windowClosed(WindowEvent e) {
                Indirizzo indirizzo = indirizzoForm.getIndirizzo();
                if (indirizzo != null) {
                    clientefornitoreEntity.setIndirizzoTitolare(indirizzo);
                }
                aggiornaCampoIndirizzo(jTextField12, indirizzo);
                abilita();
            }

            @Override
            public void windowIconified(WindowEvent e) {
                
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
                
            }

            @Override
            public void windowActivated(WindowEvent e) {
                
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
                
            }
        });
    }//GEN-LAST:event_jTextField12MouseClicked

    private void jXDatePicker1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jXDatePicker1PropertyChange
        if (jXDatePicker1.getDate() == null) {
            return;
        }
        Calendar valoreDataNascita = Calendar.getInstance();
        valoreDataNascita.setTime(jXDatePicker1.getDate());
        clientefornitoreEntity.setDataNascita(valoreDataNascita);
    }//GEN-LAST:event_jXDatePicker1PropertyChange

    private void jXDatePicker2PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jXDatePicker2PropertyChange
        if (jXDatePicker2.getDate() == null) {
            return;
        }
        Calendar valoreDataDoc = Calendar.getInstance();
        valoreDataDoc.setTime(jXDatePicker2.getDate());
        clientefornitoreEntity.setDataDoc(valoreDataDoc);
    }//GEN-LAST:event_jXDatePicker2PropertyChange

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        FrontController.stampaClienteFornitore(clientefornitoreEntity.getIdClienteFornitore().intValue());
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField12;
    private javax.swing.JTextField jTextField13;
    private javax.swing.JTextField jTextField14;
    private javax.swing.JTextField jTextField15;
    private javax.swing.JTextField jTextField16;
    private javax.swing.JTextField jTextField17;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    private org.jdesktop.swingx.JXDatePicker jXDatePicker1;
    private org.jdesktop.swingx.JXDatePicker jXDatePicker2;
    private org.jdesktop.swingx.JXDatePicker jXDatePicker3;
    // End of variables declaration//GEN-END:variables

    private boolean validaCampi() {
      if   (validaCampo(jTextField1) && 
             
            validaCampo(jTextField11) && 
            validaCampo(jTextField12) && 
            validaCampo(jTextField13) &&  
            validaCampo(jTextField14) && 
            validaCampo(jTextField15) && 
            validaCampo(jTextField8) && 
        
            validaCampo(jTextField2) && 
            validaCampo(jTextField4) && 
            validaCampo(jTextField3) && 
            validaCampo(jTextField5) && 
            validaCampo(jTextField6) && 
            validaCampo(jTextField7) && 
            validaCampo(jTextField8) && 
            validaCampo(jTextField9) && 
            validaCampo(jXDatePicker1) && 
            validaCampo(jXDatePicker3) && 
            validaCampo(jXDatePicker2)) {
          return true;
      }
      return false;
        
    }
    private boolean validaCampo(JComponent c) {
        
        return c.getInputVerifier().verify(c);
    }
    private void aggiornaCampoIndirizzo(JTextField jTextField, Indirizzo indirizzoInserito) {
        String stringa = "";
        if ((indirizzoInserito == null) || (indirizzoInserito.getVia() == null)) {
            stringa = "";
        } else {
            stringa = indirizzoInserito.getVia() + 
                ", n."
                + indirizzoInserito.getNumeroCivico() 
                + " "
                + indirizzoInserito.getCap()
                + " "
                + indirizzoInserito.getCitta()
                + "("
                + indirizzoInserito.getProvincia()
                + ")";
        }
        
                jTextField.setText(stringa);
    }
    private void abilita() {
        this.setEnabled(true);
    }
}
